$(function () {
    let code;

    // 点击发送验证码按钮
    $(".register-code").click(function (e) {
        e.preventDefault(); // 阻止默认提交行为
        const phone = $(".phone").val();

        // 如果按钮被禁用，则直接返回
        if (this.disabled) return;

        let countdown = 60; // 倒计时初始值
        this.disabled = true; // 禁用按钮
        this.innerText = `${countdown}秒后重发`; // 显示倒计时文本
        const button = this; // 保存按钮的引用以便定时器中使用

        // 开始倒计时
        const timer = setInterval(() => {
            countdown--;
            if (countdown <= 0) {
                clearInterval(timer); // 停止倒计时
                button.disabled = false; // 启用按钮
                button.innerText = "发送验证码"; // 恢复按钮文本
            } else {
                button.innerText = `${countdown}秒后重发`;
            }
        }, 1000);

        // Ajax 发送验证码请求
        $.ajax({
            url: "http://localhost:8080/sms/code",
            method: "post",
            data: { phone },
            success: function (data) {
                code = data.t;
                console.log(code);

            },
            error: function () {
                alert("验证码发送失败，请重试！");
                clearInterval(timer); // 如果请求失败，停止倒计时
                button.disabled = false;
                button.innerText = "发送验证码";
            },
        });
    });

    // 点击注册按钮
    $("#regiser").click(function (event) {
        event.preventDefault();

        const nickname = $("#nickname").val();
        const password = $("#password").val();
        const surePassword = $("#surePassword").val();
        const phone = $(".phone").val();
        const agreeTerms = $("#agreeTerms").is(":checked");

        if (password !== surePassword) {
            alert("两次密码不一致，请重新输入！");
            return;
        }
        if (!agreeTerms) {
            alert("您必须同意所有条款才能注册！");
            return;
        }
        if (code != $("#code").val()) {
            alert("验证码有误！");
            return;
        }

        // 发送注册请求
        $.ajax({
            url: "http://localhost:8080/user/register",
            type: "post",
            dataType: "json",
            data: {
                userNickname: nickname,
                userPassword: password,
                phone,
            },
            success: function (rs) {
                if (rs.code === 2001) {
                    alert('注册成功！')
                    window.location.href = "login.html";
                } else {
                    alert(rs.message);
                }
            },
            error: function (xhr, status, error) {
                alert("请求失败！");
                console.error("Error:", status, error);
            },
        });
    });
});

// 动态启用/禁用发送验证码按钮
document.addEventListener("DOMContentLoaded", function () {
    const phoneInput = document.querySelector(".phone");
    const agreeTermsCheckbox = document.getElementById("agreeTerms");
    const sendCodeButton = document.getElementById("sendCode");

    function updateButtonState() {
        const isPhoneValid = phoneInput.value.trim() !== "";
        const isTermsAgreed = agreeTermsCheckbox.checked;
        sendCodeButton.disabled = !(isPhoneValid && isTermsAgreed);
    }

    phoneInput.addEventListener("input", updateButtonState);
    agreeTermsCheckbox.addEventListener("change", updateButtonState);

    // 初始化按钮状态
    updateButtonState();
});
